﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

namespace Galaxy.AutoCAD.Strings
{
    public static class StringTool
    {
        #region 随机字符串
        public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
        {
            byte[] array = new byte[4];
            new RNGCryptoServiceProvider().GetBytes(array);
            Random random = new Random(BitConverter.ToInt32(array, 0));
            string text = null;
            string text2 = "";
            if (useNum)
            {
                text2 += "0123456789";
            }
            if (useLow)
            {
                text2 += "abcdefghijklmnopqrstuvwxyz";
            }
            if (useUpp)
            {
                text2 += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            }
            if (useSpe)
            {
                text2 += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
            }
            for (int i = 0; i < length; i++)
            {
                text += text2.Substring(random.Next(0, text2.Length - 1), 1);
            }
            return custom + text;
        }
        #endregion
        #region 判断是否是大写字母
        public static bool IsUpLetter(string myString)
        {
            return Regex.IsMatch(myString, "[A-Z]+$");
        }
        #endregion
        #region 判断字符串是否是数字(包括负号)
        public static bool IsNumber(string myString)
        {
            Regex regex = new Regex("[^0-9.-]");
            Regex regex2 = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex regex3 = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            string text = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            string text2 = "^([-]|[0-9])[0-9]*$";
            Regex regex4 = new Regex(string.Concat(new string[]
            {
                "(",
                text,
                ")|(",
                text2,
                ")"
            }));
            return !regex.IsMatch(myString) && !regex2.IsMatch(myString) && !regex3.IsMatch(myString) && regex4.IsMatch(myString);
        }
        #endregion
        #region 判断字符串是否符合叠合板名称命名规则
        public static bool IsSafeLaminateName(string laminateName)
        {
            bool result = false;
            if (laminateName.Length >= 5)
            {
                string lastThreeString = laminateName.Substring(laminateName.Length - 3, 3);
                string remainingString = laminateName.Substring(0, laminateName.Length - 3);
                if (StringTool.IsNumber(lastThreeString) && StringTool.IsUpLetter(remainingString))
                {
                    result = true;
                }
            }
            return result;
        }
        #endregion
        #region 判断字符串是否符合块命名规则
        public static bool IsSafeBlockString(string myString)
        {
            return !Regex.IsMatch(myString, "[|;|,|\\/|||||\\}|\\{|%|@|\\*|!|\\']");
        }
        #endregion
        #region 叠合板名称编号补零操作
        public static string RepairZeroToString(string myString)
        {
            string result = "001";
            if (myString != null)
            {
                if (myString != "")
                {
                    if (StringTool.IsNumber(myString))
                    {
                        if (myString.Count<char>() == 1)
                        {
                            result = "00" + myString;
                        }
                        else
                        {
                            if (myString.Count<char>() == 2)
                            {
                                result = "0" + myString;
                            }
                            else
                            {
                                if (myString.Count<char>() == 3)
                                {
                                    result = myString;
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }
        #endregion
    }
}
